local extension = Package:new("wq_anchao")
extension.extensionName = "FkWqmt"
Fk:loadTranslationTable{
  ["wq_anchao"] = "无期迷途·暗潮",
}

local Wqc = require "packages/FkWqmt/wq_common"
local U = require "packages/utility/utility"

-- 禁闭者：安吉尔
local wq_anjier = General:new(extension, "wq_anjier", "qun", 3, 3, General.Female)
-- 技能：刃影
local wq_skill_renying = fk.CreateTriggerSkill{
  name = "wq_skill_renying",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      if player.phase == Player.Start then
        return player:hasSkill(self) and target == player
      end
    elseif event == fk.AfterCardsMove then
      if player:hasSkill(self) then
        for _, move in ipairs(data) do
          if move.from and room:getPlayerById(move.from).phase == Player.NotActive then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerEquip then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#wq_skill_renying_prompt-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 1, self.name)
    if not player:isKongcheng() then
      local cards = room:askForCard(player, 1, 1, false, self.name, false, ".", "#wq_skill_renying_prompt-choose")
      if #cards > 0 then
        for _, id in ipairs(player.player_cards[Player.Hand]) do
          local card = Fk:getCardById(id)
          if table.contains(cards, id) then
            room:setCardMark(card, "@@wq_skill_renying_mark", 1)
          end
        end
      end
    end
  end,
  refresh_events = {fk.GameStart, fk.AfterCardsMove, fk.EventLoseSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self) and not player:isKongcheng()
    elseif event == fk.AfterCardsMove then
      return true
    elseif event == fk.EventLoseSkill then
      return target == player and data == self
    else
      return false
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local cards = player:getCardIds(Player.Hand)
      for _, id in ipairs(cards) do
        room:setCardMark(Fk:getCardById(id), "@@wq_skill_renying_mark", 1)
      end
    elseif event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.toArea ~= Card.Processing then
          for _, info in ipairs(move.moveInfo) do
            room:setCardMark(Fk:getCardById(info.cardId), "@@wq_skill_renying_mark", 0)
          end
        end
      end
    elseif event == fk.EventLoseSkill then
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        room:setCardMark(Fk:getCardById(id), "@@wq_skill_renying_mark", 0)
      end
    end
  end,
}
local wq_skill_renying_maxcards = fk.CreateMaxCardsSkill{
  name = "#wq_skill_renying_maxcards",
  exclude_from = function(self, player, card)
    return card:getMark("@@wq_skill_renying_mark") > 0
  end,
}
wq_skill_renying:addRelatedSkill(wq_skill_renying_maxcards)
-- 技能：破敌
local wq_skill_podi = fk.CreateTriggerSkill{
  name = "wq_skill_podi",
  anim_type = "offensive",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.tos then
      if table.find(TargetGroup:getRealTargets(data.tos), function(id) return id ~= player.id and not player.room:getPlayerById(id).dead end) then
        if (data.card.trueName == "slash" or data.card:isCommonTrick()) and (not player:isKongcheng()) then
          if player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and player.phase == Player.Play then
            return true
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(TargetGroup:getRealTargets(data.tos), function(id) return not room:getPlayerById(id).dead end)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#wq_skill_podi_prompt:::"..data.card:toLogString(), self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local cards = table.filter(player:getCardIds("h"), function(id)
      local card = Fk:getCardById(id)
      return (card.trueName == "slash" or card:isCommonTrick()) and card:getMark("@@wq_skill_renying_mark") > 0
    end)
    if #cards > 0 then
      cards = table.reverse(cards)
      for i = #cards, 1, -1 do
        if player.dead or to.dead or player:isKongcheng() then
          break
        end
        if table.contains(player:getCardIds("h"), cards[i]) then
          local card = Fk:getCardById(cards[i])
          if not player:isProhibited(to, card) then
            room:useCard({
              from = player.id,
              tos = {{to.id}},
              card = card,
              extraUse = true,
            })
          end
        end
      end
    end
  end,
}
-- 技能：隐匿
local wq_skill_yinni = fk.CreateTriggerSkill{
  name = "wq_skill_yinni",
  anim_type = "defensive",
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#wq_skill_yinni_prompt")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|spade",
    }
    room:judge(judge)
    local result = judge.card
    if result.suit == Card.Spade then
      data.damage = data.damage - 1
      if data.damage < 1 then
        return true
      end
    end
  end,
}
-- 安吉尔技能
wq_anjier:addSkill(wq_skill_renying)
wq_anjier:addSkill(wq_skill_podi)
wq_anjier:addSkill(wq_skill_yinni)
-- 安吉尔翻译
Fk:loadTranslationTable{
  ["wq_anjier"] = "安吉尔",
  ["#wq_anjier"] = "超能直感",
  ["designer:wq_anjier"] = "妙霄",
  ["cv:wq_anjier"] = "官方",
  ["illustrator:wq_anjier"] = "官方",
  ["wq_skill_renying"] = "刃影",
  [":wq_skill_renying"] = "游戏开始时，你的初始手牌增加「刃影」标记。准备阶段或当一名角色于其回合外失去装备区里的牌后，你可以摸一张牌，然后为一张手牌增加「刃影」标记。有「刃影」标记的手牌不计入你的手牌上限。",
  ["$wq_skill_renying"] = "利刃一旦出了鞘，就必定是要见血的。",
  ["@@wq_skill_renying_mark"] = "刃影",
  ["#wq_skill_renying_prompt-invoke"] = "刃影：你可以摸一张牌并为一张手牌增加「刃影」标记",
  ["#wq_skill_renying_prompt-choose"] = "刃影：请选择一张手牌",
  ["wq_skill_podi"] = "破敌",
  [":wq_skill_podi"] = "出牌阶段限一次，你对其他角色使用【杀】或普通锦囊牌结算后，你可以对其中一个目标使用手牌中所有带有「刃影」标记的【杀】和普通锦囊牌（无距离和次数限制）。",
  ["$wq_skill_podi"] = "这条命我收下了！",
  ["#wq_skill_podi_prompt"] = "破敌：你可以对%arg的一名目标角色使用手牌中所有带有「刃影」标记的【杀】和普通锦囊牌（无距离和次数限制）",
  ["wq_skill_yinni"] = "隐匿",
  [":wq_skill_yinni"] = "当你受到伤害时，你可以判定。若结果为黑桃，你防止此伤害。",
  ["$wq_skill_yinni"] = "这把刀跟我，谁会先被折断呢？",
  ["#wq_skill_yinni_prompt"] = "隐匿：你可以判定，若结果为黑桃，你防止此伤害",
  ["~wq_anjier"] = "呵呵……雇主要失望了……",
}

-- 禁闭者：戈蓝
local wq_gelan = General:new(extension, "wq_gelan", "qun", 4, 4, General.Female)
-- 技能：恣意
local wq_skill_ziyi = fk.CreateTriggerSkill{
  name = "wq_skill_ziyi",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local num = math.min(5, player.hp + 1 - player:getHandcardNum())
    return player.room:askForSkillInvoke(player, self.name, data, "#wq_skill_ziyi_prompt:::"..num)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, 1)
    room:recover {
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name
    }
    local num = math.min(5, player.hp - player:getHandcardNum())
    if num > 0 then
      room:drawCards(player, num, self.name)
    end
    local slash_ids = table.filter(player.player_cards[Player.Hand], function(cid) return Fk:getCardById(cid).trueName == "slash" end)
    room:throwCard(slash_ids, self.name, player)
    U.askForUseVirtualCard(room, player, "slash", nil, self.name, nil, true, true, true, true)
  end,
}
-- 技能：霰弹
local wq_skill_xiandan = fk.CreateTriggerSkill{
  name = "wq_skill_xiandan",
  anim_type = "control",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if target == player and player:hasSkill(self) and data.tos then
      if table.find(TargetGroup:getRealTargets(data.tos), function(id) return id ~= player.id and not room:getPlayerById(id).dead end) then
        if data.card.trueName == "slash" and player.maxHp > 1 then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, data, "#wq_skill_xiandan_prompt:::"..data.card:toLogString())
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -1)
    local dests = table.map(TargetGroup:getRealTargets(data.tos), function(id) return room:getPlayerById(id) end)
    for _, dest in ipairs(dests) do
      if dest:isAlive() then
        room:addPlayerMark(dest, "@wq_mark_pozhan", 1)
      end
    end
  end,
}
-- 技能：混战
local wq_skill_hunzhan = fk.CreateTriggerSkill{
  name = "wq_skill_hunzhan",
  anim_type = "support",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.NotActive and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#wq_skill_hunzhan_prompt")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, 1)
  end,
}
-- 戈蓝的技能
wq_gelan:addSkill(wq_skill_ziyi)
wq_gelan:addSkill(wq_skill_xiandan)
wq_gelan:addSkill(wq_skill_hunzhan)
-- 戈蓝的翻译
Fk:loadTranslationTable{
  ["wq_gelan"] = "戈蓝",
  ["#wq_gelan"] = "质料继承",
  ["designer:wq_gelan"] = "妙霄",
  ["cv:wq_gelan"] = "官方",
  ["illustrator:wq_gelan"] = "官方",
  ["wq_skill_ziyi"] = "恣意",
  [":wq_skill_ziyi"] = "结束阶段，你可以增加1点体力上限，回复1点体力，然后将手牌数摸至与体力值相同（至多摸5张），并弃置其中的所有【杀】。若如此做，你可以视为使用一张无距离限制的【杀】。",
  ["$wq_skill_ziyi"] = "敢欺负我罩着的人，找死！",
  ["#wq_skill_ziyi_prompt"] = "恣意：你可以增加1点体力上限，回复1点体力，然后摸%arg张牌，并弃置其中的所有【杀】。<br>若如此做，你可以视为使用一张无距离限制的【杀】。",
  ["wq_skill_xiandan"] = "霰弹",
  [":wq_skill_xiandan"] = "你使用的【杀】结算完毕后，若你的体力上限大于1，你可以减少1点体力上限，令此【杀】的所有目标分别获得1枚“破绽”标记。"
  ..Wqc.getTranslateInNewLine("#wq_common_mark_pozhan_description"),
  ["$wq_skill_xiandan"] = "刚才是你这家伙在挑事儿是吧！还叫不叫了？",
  ["#wq_skill_xiandan_prompt"] = "霰弹：你可以减少1点体力上限，令此%arg的所有目标分别获得1枚“破绽”标记",
  ["wq_skill_hunzhan"] = "混战",
  [":wq_skill_hunzhan"] = "每名其他角色的回合限一次，当你使用牌时，你可以增加1点体力上限。",
  ["$wq_skill_hunzhan"] = "哈？是什么货色要我亲自出手？",
  ["#wq_skill_hunzhan_prompt"] = "混战：你可以增加1点体力上限",
  ["~wq_gelan"] = "至少这次……我没夹着尾巴……逃走……",
}

return extension